package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.web.mgr.UserIdMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/queryServerRoleCreate")
public class QueryServerRoleCreate extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(QueryServerRoleCreate.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request queryServerRoleCreate");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		int type = 1;
		if(paramObj.containsKey("type")){
			type = paramObj.getIntValue("type");
		}
		JSONObject dataObj = new JSONObject();
		long roleCreateCountLimit = 0;
		ServerInfo serverInfo = ServerListMgr.getServerInfo(WebServer.getInstance().getServerId());
		if(null != serverInfo) {
			roleCreateCountLimit = serverInfo.getCreateRoleCountLimit();
		}
		dataObj.put("role", UserIdMgr.getTotalRoleCreateCount());
		if(type == 2){
			dataObj.put("role", UserIdMgr.getTotalRoleCreateCount2());
		}else if(type == 3){
			dataObj.put("role", UserMgr.getRegisterNum());
		}
		dataObj.put("limit", roleCreateCountLimit);

		JSONObject resultObj = new JSONObject();
		resultObj.put("" + WebServer.getInstance().getServerId(), dataObj);

		sendMessage(response, resultObj.toJSONString());
	}
}
